import { Component, OnInit } from '@angular/core';
import { Carousel } from '../../model/carousel';
import { NewsService } from '../../service/news.service';
import { Router, ActivatedRoute } from '@angular/router';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { TranslationWidth } from '@angular/common';
import { CarouselService } from '../../service/carousel.service';

@Component({
  selector: 'app-eidtcarousel',
  templateUrl: './eidtcarousel.component.html',
  styleUrls: ['./eidtcarousel.component.css']
})
export class EidtcarouselComponent implements OnInit {
   carousel:Carousel;
   parm:string;
   selectedFile:string;
  constructor(
    private http:HttpClient,
    private router:Router
    ,private routerinfo:ActivatedRoute,
    private carouselservice:CarouselService
  ) {

this.carousel=new Carousel();
this.parm=this.routerinfo.snapshot.params['id'];


   }

  ngOnInit() {
  }



  save(){

    this.carouselservice.addCarousel(this.carousel)
    .then((flag:boolean)=>{
if(flag==true){

  alert('成功');
}
else{
  alert('失败');
}

    })


  }


  
  onFileChanged(event) {
    this.selectedFile = event.target.files[0];
    //this.onUpload();
  }


  onUpload() {
    // upload code goes here

    const uploadData = new FormData();
  uploadData.append('file', this.selectedFile);


  this.http.post('http://localhost/manage/fileUpload',uploadData).subscribe(
    (data:any)=>{
      //alert(data);
      //console.log(JSON.stringify(data));
 let host="http://localhost/";
 this.carousel.pic=host+data.pic;

    },(err:HttpErrorResponse)=>{
      console.log(err.message);
    }
  )

  }

}
